package com.lecture.java8.excel.template;

import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.ExcelWriter;
import com.alibaba.excel.enums.WriteDirectionEnum;
import com.alibaba.excel.write.metadata.WriteSheet;
import com.alibaba.excel.write.metadata.fill.FillConfig;
import com.alibaba.excel.write.metadata.fill.FillWrapper;

import java.util.*;

public class TemplateDemo1 {
    public static void main(String[] args) {
        String templatePath = "D:\\study\\code\\spring-lecture\\src\\main\\java\\com\\example\\java8\\excel\\template\\template.xlsx";

        String fileName = "D:\\workspace\\templateTest2.xlsx";

        ExcelWriter excelWriter = null;
        try {
            excelWriter = EasyExcel.write(fileName).withTemplate(templatePath).build();
            WriteSheet writeSheet = EasyExcel.writerSheet().build();
            FillConfig fillConfig = FillConfig.builder().direction(WriteDirectionEnum.HORIZONTAL).build();
            // 如果有多个list 模板上必须有{前缀.} 这里的前缀就是 data1，然后多个list必须用 FillWrapper包裹
            excelWriter.fill(new FillWrapper("data1", data()), fillConfig, writeSheet);
            excelWriter.fill(new FillWrapper("data2", data()), writeSheet);
            excelWriter.fill(new FillWrapper("data3", data()), writeSheet);

            Map<String, Object> map = new HashMap<>();
            map.put("date", new Date());

            excelWriter.fill(map, writeSheet);
        } finally {
            // 千万别忘记关闭流
            if (excelWriter != null) {
                excelWriter.finish();
            }
        }
    }

    public static List<FillData> data() {
        List<FillData> list = new ArrayList<>();

        FillData f1 = new FillData();
        f1.setName("张三");
        f1.setDate(new Date());
        f1.setNumber(1.4D);

        /*FillData f2 = new FillData();
        f2.setName("张三");
        f2.setDate(new Date());
        f2.setNumber(1.4D);

        FillData f3 = new FillData();
        f3.setName("张三");
        f3.setDate(new Date());
        f3.setNumber(1.4D);

        FillData f4 = new FillData();
        f4.setName("张三");
        f4.setDate(new Date());
        f4.setNumber(1.4D);*/

        list.add(f1);
        //list.add(f2);
        //list.add(f3);
        //list.add(f4);

        return list;
    }


}
